package com.igeek.ch02.controller;

import com.igeek.ch02.common.CommonResult;
import com.igeek.ch02.entity.User;
import com.igeek.ch02.service.UserService;
import com.igeek.ch02.utils.JSONUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 如何处理图片上传？
 1.Servlet标注上@MultipartConfig注解，代表可以处理文件上传操作
 2.request.getParameter("key")收集普通域的数据
 request.getPart("pic");  收集文件上传域的数据
 3.前端页面中，封装FormData数据
 */
@MultipartConfig
@WebServlet(name = "UserRegisterServlet" , urlPatterns = "/register")
public class UserRegisterServlet extends HttpServlet {

    private UserService userService = new UserService();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.处理请求、响应的编码集
        //request.setCharacterEncoding("UTF-8");
        //response.setContentType("application/json;charset=UTF-8");

        //2.接收请求参数  key/value
        //解析正常表单数据
        String name = request.getParameter("name");
        String pwd = request.getParameter("pwd");
        String gender = request.getParameter("gender");
        String birthdayStr = request.getParameter("birthday");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date birthday = null;
        try {
            birthday = sdf.parse(birthdayStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //封装User对象
        User user = new User(name , gender , birthday , pwd);

        //解析图片文件数据
        Part part = request.getPart("pic");
        if(part!=null){
            String oldName = part.getHeader("content-disposition");
            //oldName = form-data; name="pic"; filename="user.jpg"
            System.out.println("oldName = "+oldName);
            if(oldName!=null && oldName.indexOf(".")>0){
                String newName = UUID.randomUUID()+oldName.substring(oldName.lastIndexOf(".") , oldName.length()-1);

                //将头像信息封装至User对象
                user.setPic("/img/"+newName);
                //将上传的图片信息写至，图片服务器上
                part.write("G:\\temp\\img\\"+newName);
            }
        }

        //3.执行业务逻辑
        CommonResult commonResult = null;
        if(user!=null){
            boolean flag = userService.register(user);
            if(flag){
                commonResult = new CommonResult(200 , "注册成功" , flag);
            }else{
                commonResult = new CommonResult(477 , "注册失败" , flag);
            }
        }

        //4.响应json数据格式的结果
        JSONUtils.writeJson(response , commonResult);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
